<template>
  <div>
    <el-card style="margin: 20px 0">
      <!-- 发起事件获得三级列表id -->
      <CategoryList @getCategoryList="getCategoryList" :show="isTableShow"></CategoryList>
    </el-card>
    <!-- 三级列表商品展示 -->
    <el-card>
      <div v-show="isTableShow">
        <!-- 添加属性按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addAttrEvent"
          >添加属性</el-button
        >
        <!-- 商品属性展示 -->
        <el-table :data="attrList" border style="width: 100%">
          <el-table-column type="index" width="80" label="序号" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称"> </el-table-column>
          <el-table-column label="属性值列表" width="width">
            <template slot-scope="{ row, $index }">
              <el-tag
                type="success"
                v-for="attrValue in row.attrValueList"
                :key="attrValue.id"
                style="margin-right: 10px"
                >{{ attrValue.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加属性界面 -->
      <div v-show="!isTableShow">
        <el-form :inline="true" label-width="80px" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input
              placeholder="请输入属性名"
              v-model="attrInfo.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-top: 10px"
          :disabled="!attrInfo.attrName"
          @click="addAttrList"
          >添加属性</el-button
        >
        <el-button type="warning" @click="isTableShow = true">取消</el-button>
        <el-table
          style="width: 100%; margin-top: 10px"
          border
          :data="attrInfo.attrValueList"
        >
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column label="属性名称" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 切换查看模式和编辑模式 -->
              <el-input
                :ref="$index"
                v-model="row.valueName"
                placeholder="请输入属性名称"
                v-if="row.flag"
                @blur="toLook(row)"
                @keyup.native.enter="toLook(row)"
              ></el-input>
              <div v-else @click="toEdit(row, $index)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-popconfirm :title="`确定删除${row.valueName}属性吗`" @onConfirm="DeleteAttr($index)">
              <el-button type="warning" icon="el-icon-delete" slot="reference" ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" style="margin-top: 10px"  @click="AddOrUpdateAttr" :disabled="attrInfo.attrValueList.length<1">保存</el-button>
        <el-button type="warning" @click="isTableShow = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
// 按需引入lodash
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Attr",
  props: {},
  components: {},
  data() {
    return {
      // 三级分类ID
      category1Id: "",
      category2Id: "",
      category3Id: "",
      // 平台属性列表信息
      attrList: {},
      // 是否显示展示列表
      isTableShow: true,
      // 添加平台属性信息
      attrInfo: {
        // 属性名
        attrName: "",
        // 属性名称列表
        attrValueList: [],
        // 商品分类id
        categoryId: 0,
        // 商品分类级数
        categoryLevel: 3,
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 获得三级列表分类ID
    getCategoryList({ categoryId, level }) {
      // 判断分类级数
      if (level === 1) {
        // 清除数据
        this.category2Id = "";
        this.category3Id = "";
        // 获得分类id
        this.category1Id = categoryId;
      } else if (level === 2) {
        // 清除数据
        this.category3Id = "";
        // 获得分类id
        this.category2Id = categoryId;
      } else if (level == 3) {
        this.category3Id = categoryId;
        // 获得商品属性
        this.getAttrList();
      }
    },
    // 获取平台属性列表功能
    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this;
      const res = await this.$API.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (res.code == 200) {
        this.attrList = res.data;
      }
    },
    // 添加属性功能
    addAttrList() {
      this.attrInfo.attrValueList.push({
        // 商品属性id添加属性名的id保持一致
        attrId: this.attrInfo.id,
        // 属性名称
        attrValue: "",
        // 查看模式和修改模式标志
        flag: true,
      });
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus();
      });
    },
    // 切换到添加属性页面功能
    addAttrEvent() {
      this.isTableShow = false;
      this.attrInfo = {
        // 属性名
        attrName: "",
        // 属性名称列表
        attrValueList: [],
        // 收集三级分类ID
        categoryId: this.category3Id,
        // 商品分类级数
        categoryLevel: 3,
      };
    },
    // 更新商品属性功能
    updateAttr(row) {
      // 切换到添加属性界面
      this.isTableShow = false;
      // 深拷贝修改的商品属性值
      this.attrInfo = cloneDeep(row);
      // 给已有的属性值添加一个响应式数据flag,实现编辑模式和响应模式
      this.attrInfo.attrValueList.forEach((item) => {
        this.$set(item, "flag", false);
      });
    },
    // 切换至查看模式
    toLook(row) {
      // 判断是否为空
      if (row.valueName.trim() === "") {
        this.$message("请输入正确的属性值");
        return;
      }
      // 判断是否有重复值
      let isRepeat = this.attrInfo.attrValueList.some((item) => {
        if (row !== item) return row.valueName == item.valueName;
      });
      if (isRepeat) {
        this.$message("请勿输入重复值");
        rteurn;
      }
      row.flag = false;
    },
    // 切换编辑模式
    toEdit(row, index) {
      // 切换到编辑模式
      row.flag = true;
      // 在渲染结束后聚焦
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    // 删除属性列表
    DeleteAttr(index){
      // alert(123)
      // 删除选中的属性值
        this.attrInfo.attrValueList.splice(index, 1);
    },
    // 保存属性功能
    async AddOrUpdateAttr(){
      this.attrInfo.attrValueList=this.attrInfo.attrValueList.filter(item=>{
        if(item.ValueName !==""){
          delete item.flag;
          return true
        }
      })
      console.log(this.attrInfo);
      
      try {
        // 发起更新请求
        await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo)
        // 切换页面
        this.isTableShow=true;
        this.$message({type: "success",message: "保存成功"})
        // 重新获取列表
        this.getAttrList()
      } catch (error) {
        console.log(error);
        console.error(error);
      }
    }
  },

};
</script>

<style scoped lang="less"></style>
